
--[[
	**********************
	* SvFrame - thomasfn *
	**********************
	SvFrame.lua - Loads core components
]]

svr = svr or {}

svr.Version = 1.21

svr.Metatables = {}

function svr.Print( msg )
	print( "[SvFrame] "..msg )
end

function svr.Warning( msg )
	print( "[SvFrame] [WARNING] "..msg )
end

function svr.Load( filename )
	return dofile( "lua/SvFrame/"..filename )
end

function svr.Metatable( name )
	if (svr.Metatables[ name ]) then
		return svr.Metatables[ name ]
	end
	local o = {}
	svr.Metatables[ name ] = o
	return o
end

function svr.ApplyMetatable( obj, name )
	local old = getmetatable( obj )
	
	if (not old) then
		setmetatable( obj, { __index = svr.Metatable( name ) } )
		svr.Print( "Created metatable and applied '" .. name .. "' to object '" .. tostring( obj ) .. "'." )
		return
	end
	if (old.__index) then
		local mt = old.__index
		if (mt.__SVFRAME__) then return end
		mt.__SVFRAME__ = true
		for key, val in pairs( svr.Metatable( name ) ) do
			mt[ key ] = val
		end
		svr.Print( "Merged metatable and applied '" .. name .. "' to object '" .. tostring( obj ) .. "'." )
		return
	end
	svr.Print( "Set metatable and applied '" .. name .. "' to object '" .. tostring( obj ) .. "'." )
	old.__index = svr.Metatable( name )
end

function svr.Enum( header, tbl, l )
	
	for key, value in pairs( tbl ) do
		if (type( value ) == "table") then
			svr.Enum( header .. "_" .. key, value )
		else
			_G[ header:upper() .. "_" .. key:upper() ] = value
		end
	end
	
	if (l) then
		svr.Lookup( header, tbl )
	end

end

function svr.Lookup( name, tbl )
	local tmp = {}
	local ParseTable
	function ParseTable( h, t )
		for key, val in pairs( t ) do
			if (type( key ) == "string") then
				if (type( val ) == "table") then
					ParseTable( h .. key:lower(), val )
				else
					tmp[ h .. key:lower() ] = val
				end
			else
				tmp[ key ] = val
			end
		end
	end
	ParseTable( "", tbl )
	local meta = {}
	meta.__index = function( tbl, key )
		--print( "Lookup table utilised! (" .. tostring( key ) .. ")" )
		if (type( key ) == "string") then
			return rawget( tmp, key:lower() )
		else
			return rawget( tmp, key )
		end
	end
	setmetatable( tmp, meta )
	--svr.PrintTable( tmp )
	_G[ "LOOKUP_" .. name:upper() ] = tmp
end

function svr.PrintTable( tbl, ignore, lvl )
	ignore = ignore or {}
	lvl = lvl or 0
	if (ignore[ tbl ]) then return end
	ignore[ tbl ] = true
	local str = ""
	for i=1, lvl do str = str .. " " end
	for key, val in pairs( tbl ) do
		if (type( val ) == "table") then
			print( str .. tostring( key ) .. ":" )
			svr.PrintTable( val, ignore, lvl + 1 )
		elseif (type( val ) == "string") then
			print( str .. tostring( key ) .. " = \"" .. val .. "\"" )
		else
			print( str .. tostring( key ) .. " = " .. tostring( val ) )
		end
	end
end

svr.Print( "Loading v" .. tostring( svr.Version ) .. "..." )

svr.Load( "enums.lua" )
svr.Load( "util.lua" )
svr.Load( "player.lua" )
svr.Load( "ihook.lua" )
svr.Load( "json.lua" )
require( "json" )
svr.Load( "commands.lua" )
svr.Load( "usergroups.lua" )

svr.Load( "plugins.lua" )